﻿Chenxin.UI.UserTransManage = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: 'service/UserTransManage.ashx',
    createGrid: function() {
        var _this=this;
        return new Chenxin.UI.JsonGrid({
            actionUrl:this.actionUrl + '?op=getlist',
            pageSize: 50,
            height:400,
            renderTo: 'UserTrans_table',
            id: 'UserTrans_table',
            cellBorder: true,
//            baseParam: { showtype: 10 },
            hasCheckBox: false,
            scope: this,
            exportAble:true,
//            cellAlign:'left',
            modelId:'RecordID',
//            title:'人数变动列表',
//            autoLoad:false,
            columns: {
                usercode: {
                    head: '员工工号',
                    width: 100,
                    sortable: true,
                    sortField: 'b.usercode'
                },
                UserName: {
                    head: '员工姓名',
                    width: 80,
                    sortable: true,
                    sortField:'a.username'
                },
                City_O: {
                    head: '原城市',
                    width: 80,
                    sortable: true,
                    sortField: 'a.City_O'
                },
                Company_O: {
                    head: '原公司',
                    width: 80,
                    sortable: true,
                    sortField: 'a.Company_O'
                },
                Depart_O: {
                    head: '原部门',
                    width: 120,
                    sortable: true,
                    sortField: 'a.Depart_O'
                },
                Position_O: {
                    head: '原职务',
                    width: 80,
                    sortable: true,
                    sortField: 'a.Position_O'
                },
                EmpLevel_O: {
                    head: '原职级',
                    width: 80,
                    sortable: true,
                    sortField: 'a.EmpLevel_O'
                },
                City_N: {
                    head: '新城市',
                    width: 80,
                    sortable: true,
                    sortField: 'a.City_N'
                },
                Company_N: {
                    head: '新公司',
                    width: 80,
                    sortable: true,
                    sortField: 'a.Company_N'
                },
                Depart_N: {
                    head: '新部门',
                    width: 120,
                    sortable: true,
                    sortField: 'a.Depart_N'
                },
                Position_N: {
                    head: '新职务',
                    width: 80,
                    sortable: true,
                    sortField: 'a.Position_N'
                },
                EmpLevel_N: {
                    head: '新职级',
                    width: 80, 
                    sortable: true,
                    sortField: 'a.EmpLevel_N'
                },
                TransType: {
                    head: '变动类型',
                    width: 80,
                    sortable: true,
                    sortField: 'TransType'
                },
                TransTime: {
                    head: '变动时间',
                    width: 80,
                    sortable: true,
                    sortField: 'TransTime'
                },
                AddTime: {
                    head: '建档日期',
                    width: 80,
                    sortable: true,
                    sortField: 'a.AddTime'
                }
            }
		});
    },
	initEvent: function() {
        var _this = this;
        $("#aEdit").live("click", function() {
            _this.edit();
        });
        $('#aDel').live("click", function() {
            _this.del();
        });
        //档案查看
        $('#aArchives').live("click", function() {
            if (_this.currentId == "") {
                alert("请选择一行后查看档案");
                return;
            }
            window.open("PersonnelFile.aspx?userid=" + _this.grid.getCurrentRecord().UserId);
        });
//        $('#aExport').click(function() {
//            _this.exportData();
//        });
    },
    createForm: function() {
        return new Chenxin.UI.UserTransEditForm({
            scope: this,
            afterShow:function(){
            if (!this.permit.edit_permit.editall) {
                    this.findByName('synchro').attr('disabled', 'disabled');
                }
            },
            afterGetFormJson: function(model) {
                model.RecordId = this.currentId;
                model.UserId = this.currentModel.UserId;
                if (this.form.permit.edit_permit.editall)
                    this.form.additionalParam.synchro = model.synchro;
                delete model.synchro;
            },
            beforeFitData: function(model) {
                if (model.CityId_O == '00000000-0000-0000-0000-000000000000') {
                    delete model.CityId_O;
                    delete model.City_O;
                } else {
                    var tempCombo = this.form.findByName('CompanyId_O');
                    tempCombo.param.fatherid = model.CityId_O;
                    tempCombo.reload(model.CompanyId_O);
                }


                var level_o = this.form.findByName('EmpLevel_O');
                var position_o = this.form.findByName('Position_O');
                var level_n = this.form.findByName('EmpLevel_N');
                var position_n = this.form.findByName('Position_N');

                if (model.CompanyId_O == '00000000-0000-0000-0000-000000000000') {
                    delete model.CompanyId_O;
                    delete model.Company_O;
                    level_o.clearItems();
                    position_o.clearItems();

                } else {
                    var tempCombo = this.form.findByName('DepartId_O');
                    tempCombo.param.fatherid = model.CompanyId_O;
                    tempCombo.reload(model.DepartId_O);


                    level_o.param.companyid = model.CompanyId_O;
                    level_o.reload(model.EmpLevel_O);

                    position_o.param.companyid = model.CompanyId_O;
                    position_o.reload(model.Position_O);

                }
                if (model.DepartId_O == '00000000-0000-0000-0000-000000000000') {
                    delete model.DepartId_O;
                    delete model.Depart_O;
                }
                if (model.CityId_N == '00000000-0000-0000-0000-000000000000') {
                    delete model.CityId_N;
                    delete model.City_N;
                } else {
                    var tempCombo = this.form.findByName('CompanyId_N');
                    tempCombo.param.fatherid = model.CityId_N;
                    tempCombo.reload(model.CompanyId_N);
                }
                if (model.CompanyId_N == '00000000-0000-0000-0000-000000000000') {
                    delete model.CompanyId_N;
                    delete model.Company_N;
                    level_n.clearItems();
                    position_n.clearItems();
                } else {
                    var tempCombo = this.form.findByName('DepartId_N');
                    tempCombo.param.fatherid = model.CompanyId_N;
                    tempCombo.reload(model.DepartId_N);

                    level_n.param.companyid = model.CompanyId_N;
                    level_n.reload(model.EmpLevel_N);

                    position_n.param.companyid = model.CompanyId_N;
                    position_n.reload(model.Position_N);
                }
                if (model.DepartId_N == '00000000-0000-0000-0000-000000000000') {
                    delete model.DepartId_N;
                    delete model.Depart_N;
                }
                model.TransTime = model.TransTime.split(' ')[0];
            }
        });
    },
	createSearch: function() {
        return new Chenxin.UI.UserTransManageSearch({
            scope: this
        });
    },
    initComponent: function() {
        Chenxin.UI.UserTransManage.superclass.initComponent.call(this);
    }
});

//var columns = {
//    mainHead: {
//        operate: {
//            head: '操作',
//            width: 120,
//            isLock: true,
//            inExpand: false,
//            renderer: function(val, c, row) {
//                function createBtn(txt, cls) {
//                    return '<a title="' + txt + '" r="' + row.RecordId + '" style="margin:0px 2px;cursor:pointer" class="x_btn_' + cls + '">' + txt + '</a>';
//                }
//                return createBtn('修改', 'edit') + createBtn('删除', 'del') + createBtn('查看', 'show');
//            }
//        },
//        RecordId: {
//            create: false,
//            head: 'id',
//            sortable: true
//        },
//        UserName: {
//            head: '员工姓名',
//            width: 80,
//            sortable: true
//        },
//        City_O: {
//            head: '原城市',
//            width: 80,
//            sortable: true
//        },
//        Company_O: {
//            head: '原公司',
//            width: 80,
//            sortable: true
//        },
//        Depart_O: {
//            head: '原部门',
//            width: 120,
//            sortable: true
//        },
//        Position_O: {
//            head: '原职务',
//            width: 80
//        },
//        EmpLevel_O: {
//            head: '原职级',
//            width: 80,
//            sortable: true
//        },
//        City_N: {
//            head: '新城市',
//            width: 80,
//            sortable: true
//        },
//        Company_N: {
//            head: '新公司',
//            width: 80,
//            sortable: true
//        },
//        Depart_N: {
//            head: '新部门',
//            width: 120,
//            sortable: true
//        },
//        Position_N: {
//            head: '新职务',
//            width: 80,
//            sortable: true
//        },
//        EmpLevel_N: {
//            head: '新职级',
//            width: 80
//        },
//        TransType: {
//            head: '变动类型',
//            width: 80,
//            sortable: true
//        },
//        TransTime: {
//            head: '变动时间',
//            width: 80,
//            sortable: true
//        }
//    }
//}

/**
*查询面板
*/
Chenxin.UI.UserTransManageSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'usertransmanager_search',
    initEvent: function() {
        var _this = this;
        $('#btn_search').click(function() {
            _this.scope.reloadGrid();
        });
    },
    createComboList: function() {
        var combl = new Chenxin.UI.StructComboList({ tbxWidth: 60 });
        var structs = combl.comboList;

        var cmbStatus = new Chenxin.UI.Combo({
            containerId: '#divStatus',
            tbxWidth: 45,
            items: comboCommon.UserStatusType
        });

        var cmbTransType2 = new Chenxin.UI.Combo({
            containerId: '#divTransType2',
            tbxWidth: 80,
            colWidth: 80,
            items: comboCommon.TransType2
        });

        var cmbDate = new Chenxin.UI.Combo({
            containerId: '#divdate',
            addPleaseSelected: false,
            tbxWidth: 55,
            items: [{ val: 'TransTime', txt: '变动日期' }, { val: 'a.AddTime',txt:'建档日期'}]
        });

        structs.push(cmbStatus);
        structs.push(cmbTransType2);
        structs.push(cmbDate);
        return structs;
    },
    initComponent: function() {
        Chenxin.UI.UserTransManageSearch.superclass.initComponent.call(this);
    }
});

/**
*修改变动记录层
*/
Chenxin.UI.UserTransEditForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/UserTransManage.ashx',
    id: 'jqmUserTransEdit',
    renderTo: 'divjqmUserTransEdit',
    title: '人事变动编辑',
    jqmWidth: 700,
    initFormValidator: function() {
        var _this = this;

        //变动日期
        $("#txtTransTimeChgPer").focus(function() {
            WdatePicker({ oncleared: function() { $(this).blur(); }, onpicked: function() { $(this).blur(); } })
        }).formValidator({ validatorgroup: this.id,
            onshow: "请输入变动日期!",
            onfocus: "请输入变动日期!",
            oncorrect: " "
        }).inputValidator({
            empty: { leftempty: false, rightempty: false, emptyerror: "变动日期不能为空!" },
            type: "date",
            onerror: "变动日期不能为空!"
        });

    },
    createForm: function() {
        return formCommon.UserTransEditForm();
    },
    initEvent: function() {
        var _this = this;
        //提交按钮事件
        $('#aSubmit').live('click', function() {
            _this.save();
        });
        //关闭按钮事件
        $('#aClose').live('click', function() {
            _this.hide();
        });
    },
    createComboList: function() {
        var _this = this;

        //旧城市
        var city = new Chenxin.UI.StructCombo({
            containerId: '#old_city',
            tbxWidth: 90,
            name: 'CityId_O',
            inModelType: 'all',
            txtModelName: 'City_O',
            param: { op: 'getcity' },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    company.clearItems();
                } else {
                    company.param.fatherid = this.getValue();
                    company.reload();
                }
                departs.clearItems();
            }
        });

        //旧公司
        var company = new Chenxin.UI.StructCombo({
            containerId: '#old_company',
            tbxWidth: 90,
            autoLoad: false,
            name: 'CompanyId_O',
            inModelType: 'all',
            txtModelName: 'Company_O',
            param: { op: 'getcompany' },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    departs.clearItems();
                    cmbEmpLevel.clearItems();
                    cmbEditPosition.clearItems();
                } else {
                    departs.param.fatherid = this.getValue();
                    departs.reload();

                    cmbEmpLevel.param.companyid = this.getValue();
                    cmbEmpLevel.reload();

                    cmbEditPosition.param.companyid = this.getValue();
                    cmbEditPosition.reload();
                }
                //businessDistrict.clearItems();
            }
        });


        //旧部门
        var departs = new Chenxin.UI.StructCombo({
            containerId: '#old_dep',
            tbxWidth: 90,
            colWidth: 120,
            autoLoad: false,
            required: false,
            hasSearch: true,
            name: 'DepartId_O',
            inModelType: 'all',
            txtModelName: 'Depart_O',
            hasPage: true,
            pageSize: 10,
            param: { op: 'getcompanydeparts' }
        });

        //新城市
        var city_n = new Chenxin.UI.StructCombo({
            containerId: '#new_city',
            tbxWidth: 90,
            name: 'CityId_N',
            inModelType: 'all',
            txtModelName: 'City_N',
            param: { op: 'getcity' },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    company_n.clearItems();
                } else {
                    company_n.param.fatherid = this.getValue();
                    company_n.reload();
                }
                businessDistrict_n.clearItems();
                departs_n.clearItems();
            }
        });

        //新公司
        var company_n = new Chenxin.UI.StructCombo({
            containerId: '#new_company',
            tbxWidth: 90,
            autoLoad: false,
            name: 'CompanyId_N',
            inModelType: 'all',
            txtModelName: 'Company_N',
            param: { op: 'getcompany' },
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    departs_n.clearItems();
                    cmbEmpLevel_n.clearItems();
                    cmbEditPosition_n.clearItems();
                } else {
                    departs_n.param.fatherid = this.getValue();
                    departs_n.reload();


                    cmbEmpLevel_n.param.companyid = this.getValue();
                    cmbEmpLevel_n.reload();

                    cmbEditPosition_n.param.companyid = this.getValue();
                    cmbEditPosition_n.reload();
                }
            }
        });


        //新部门
        var departs_n = new Chenxin.UI.StructCombo({
            containerId: '#new_dep',
            tbxWidth: 90,
            colWidth: 120,
            name: 'DepartId_N',
            inModelType: 'all',
            txtModelName: 'Depart_N',
            autoLoad: false,
            required: false,
            hasSearch: true,
            hasPage: true,
            pageSize: 10,
            param: { op: 'getcompanydeparts' }
        });

        //变动类型
        var cmbTransType = new Chenxin.UI.Combo({
            containerId: '#TransType',
            tbxWidth: 90,
            inModelType: 'txt',
            name: 'TransType',
            items: comboCommon.TransType1,
            required: true
        });
        //旧职级
        var cmbEmpLevel = new Chenxin.UI.ParameterCombo({
            containerId: '#old_level',
            name: 'EmpLevel_O',
            key: 'System.EmpLevel',
            inModelType: 'txt',
            tbxWidth: 90,
            colNum: 3,
            hasPage: true,
            hasSearch: true,
            pageSize: 15,
            required: false
        });
        //旧职务
        var cmbEditPosition = new Chenxin.UI.ParameterCombo({
            containerId: '#old_postion',
            inModelType: 'txt',
            name: 'Position_O',
            key: 'System.Position',
            tbxWidth: 90,
            colNum: 3,
            hasPage: true,
            hasSearch: true,
            pageSize: 15,
            required: false
        });

        //新职级
        var cmbEmpLevel_n = new Chenxin.UI.ParameterCombo({
            containerId: '#new_level',
            name: 'EmpLevel_N',
            key: 'System.EmpLevel',
            inModelType: 'txt',
            tbxWidth: 90,
            colNum: 3,
            hasPage: true,
            hasSearch: true,
            pageSize: 15,
            required: false
        });
        //新职务
        var cmbEditPosition_n = new Chenxin.UI.ParameterCombo({
            containerId: '#new_postion',
            inModelType: 'txt',
            name: 'Position_N',
            key: 'System.Position',
            tbxWidth: 90,
            colNum: 3,
            hasPage: true,
            hasSearch: true,
            pageSize: 15,
            required: false
        });

        return [city, company, departs, cmbTransType, cmbEmpLevel, cmbEditPosition, cmbEmpLevel_n, cmbEditPosition_n, city_n, company_n, departs_n];
    },
    saveSucc: function() {
        this.reloadGrid();
    },
    initComponent: function() {
        Chenxin.UI.UserTransEditForm.superclass.initComponent.call(this);
    }
});

$(document).ready(function() {
    var usertransmgr = new Chenxin.UI.UserTransManage();
});